home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7525 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  91 lines

  1. Path: newsroom.hitc.com!usenet
  2. From: Chris Ruegger <cruegger@eos.hitc.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: virtual operator==(), operator=()
  5. Date: Fri, 23 Feb 1996 14:32:12 -0500
  6. Organization: Hughes Information Technology Corporation
  7. Message-ID: <312E163C.36ED@eos.hitc.com>
  8. NNTP-Posting-Host: rainman.hitc.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.4 sun4m)
  13.  
  14. Forgive what is probably a FAQ, but what is the consensus on
  15. solving this problem: 
  16.  
  17. I have a class hierarchy. I maintain some eollection of pointers
  18. to the *BASE* class. Suppose I create a new derived objects and
  19. I want to find out if it is logically already in my collection,
  20. using operator==() 
  21.  
  22. It would seem one is forced to make operator==() virtual, check
  23. the type of the object in the parameter list, and downcast
  24. appropriately if the two objects are of the same type, and then
  25. perform the necessary operator==() logic. That is:
  26.  
  27. class BASE {
  28. public:
  29.   BASE()
  30.   virtual int operator==(const BASE& base)
  31.   { logic omitted }
  32. private:
  33.   int foo_;
  34.  
  35. class DERIVED1 : public BASE {
  36. public:
  37.   virtual int operator==(const BASE& base)
  38.   { if (*this and base are of the same type)
  39.        DERIVED1& r = *(DERIVED1*)&base;         // downcast
  40.        // now compare corresponding data members using r
  41.     }
  42.     return as appropriate
  43.    }
  44. };
  45.  
  46. class DERIVED2 : public BASE {
  47. public:
  48.   virtual int operator==(const BASE& base)
  49.   { if (*this and base are of the same type)
  50.        DERIVED2& r = *(DERIVED2*)&base;         // downcast
  51.        // now compare corresponding data members using r
  52.     }
  53.     return as appropriate
  54.    }
  55. };
  56.  
  57.  
  58. int main()
  59. {
  60.  
  61.   // Build a pre-existing list for demonstration purposes
  62.  
  63.   BASE *list[3];
  64.   DERIVED1 d1;
  65.   DERIVED2 d2;
  66.   list [0] = &d1;
  67.   list [1] = &d2;
  68.  
  69.    // Now suppose we build a new object, want to see if it's already
  70.    // in the list
  71.    DERIVED2 d3;
  72.    for (int i = 0; i < 2; i++) {
  73.       if (*list[i] == d3)  {       // Here we go
  74.          //duplicate found
  75.       }
  76.    }
  77.  
  78.  
  79. IS there a more elegant way to do this???
  80. How have others solved this problem?
  81.  
  82.  
  83. Thanks.
  84.  
  85.  
  86.  
  87. --------------------------------------------------------------------
  88. Christopher Ruegger                 phone:   (301) 925-1164
  89.                                     Email:   cruegger@eos.hitc.com
  90.                                              car@access.digex.com
  91.